<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Admin\Controller as Controller1;
use App\Http\Requests\Api\MenuRequest;
use App\Http\Requests\Api\MenuUpdateRequest;
use App\Models\Admin;
use App\Models\AdminAuth;
use App\Models\AdminRole;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class AdminController extends Controller1
{

    protected $admin;

    public function __construct()
    {
        $this->admin = Auth::guard('admin')->user();

    }

    //获取管理员权限列表
    public function getAdminMenu(){

        $role_id = $this->admin['role_id'];
        if($role_id == 0){

            $menuList = AdminAuth::get();

            return $this->success($menuList);
        }

        $role = AdminRole::find($role_id);

        $menuList = AdminAuth::whereIn('id',explode(',',$role->menus))->get();

        return $this->success($menuList);

    }

    //管理员列表
    public function getAdminList(Request $request){

        $where = function ($query) use ($request){
            if($request->name){ //管理员名字
                $query->where('name',$request->name);
            }
            if($request->role_id){//角色
                $query->where('role_id',$request->role_id);
            }
        };

        $list = Admin::with("role:id,name")->where($where)->paginate($request->pageSize?:15);

        return $this->success($list);

    }

    //添加管理员
    public function addAdmin(Request $request){

        $name = $request->name;
        $password = $request->password;
        $role_id = $request->role_id;
        $mobile = $request->mobile;
        $admin = new Admin();
        $admin->name = $name;
        $admin->user = $request->user;
        $admin->password = $password;
        $admin->role_id = $role_id;
        $admin->mobile = $mobile;
        $res = $admin->save();

        return $this->success('添加成功');


    }

    public function editAdmin(Request $request){

        $id = $request->id;

        if(!$id){
            return $this->failed("id必传");
        }

        $name = $request->name;
        $username = $request->username;
        $password = $request->password;
        $role_id = $request->role_id;
        $mobile = $request->mobile;
        $avatar_url = $request->avatar_url;
        $admin = Admin::find($id);
        $admin->username = $request->username;
        $admin->name = $name;
        $admin->password = $password;
        $admin->role_id = $role_id;
        $admin->mobile = $mobile;
        $admin->avatar_url = $avatar_url;
        $res = $admin->save();

        return $this->success('编辑成功');


    }

    //删除管理员
    public function deleteAdmin(Request $request){

        $id = $request->id;


        $res = Admin::where('id',$id)->delete();

        if($res)
            return $this->success('操作成功');

        return $this->failed('操作失败');


    }

    //添加编辑角色
    public function addRole(Request $request){

        $id = $request->id;
        $name = $request->name;
        $menus = $request->menus;
        $role_desc = $request->role_desc;

        if(!$name){
            return $this->failed('角色名称不能为空');
        }
        if(!$menus){
            return $this->failed('权限列表不能为空');
        }

        if($id){
            $res = DB::table('role')->where('id',$id)->update(['name'=>$name,'menus'=>$menus,'role_desc'=>$role_desc,'updated_at'=>date("Y-m-d H:i:s")]);

            if($res)
                return $this->success('编辑成功');

            return $this->failed("编辑失败");

        }else{

            $res = DB::table('role')->insert(['name'=>$name,'menus'=>$menus,'creator_id'=>$this->admin['id'],'role_desc'=>$role_desc,'created_at'=>date("Y-m-d H:i:s"),'updated_at'=>date("Y-m-d H:i:s")]);

            if($res)
                return $this->success('添加成功');

            return $this->failed("添加失败");


        }


    }

    //获取角色列表
    public function roleList(){
        $list = DB::table('role')->get();

        return $this->success($list);
    }
    //获取角色详情
    public function  roleInfo(Request $request){
        $id = $request->id;

        $info = DB::table('admin_role')->find($id);

        return $this->success($info);
    }


    //添加菜单
    public function addMenu(MenuRequest $request){

        $admin = Auth::guard('admin')->user();


        AdminAuth::create(array_merge($request->all(),['creator_id'=>$admin['id']]));

        return $this->success('添加成功');
    }

    //编辑菜单
    public function editMenu(MenuUpdateRequest $request){

        $adminAuth = AdminAuth::find($request->id);
        $adminAuth->fill($request->all()); //将值赋值到模型

        if($adminAuth->update()){

            return $this->success('编辑成功');

        }else{
            return $this->failed('编辑失败');
        }

    }

    //菜单列表
    public function MenuList(){
        $list = DB::table('menu')->get();
        return $this->success($list);
    }


    //删除菜单
    public function deleteMenu(Request $request){
        $id = $request->id; //广告id
        if(!$id)
            return $this->failed('id必传');
        $res = AdminAuth::where('id',$id)->delete();
        if($res)
            return $this->success('删除成功');

        return $this->failed('删除失败');

    }










}
